<template>
  <div>
    <el-card>
      <el-row>
        <el-col :span="7">
          <el-input
            placeholder="请输入商品名"
            clearable
            v-model="queryModel.query"
          >
            <el-button
              icon="el-icon-search"
              slot="append"
              @click="getGoodList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="16">
          <el-button class="button" type="primary" @click="addGoods"
            >添加商品</el-button
          >
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="#" type="index"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="180">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)">
        </el-table-column>
        <el-table-column prop="goods_number" label="商品重量">
        </el-table-column>
        <el-table-column prop="add_time" label="创建时间">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="mini">编辑</el-button>
            <el-button
              type="danger"
              size="mini"
              @click="delt(scope.row.goods_id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryModel.pagenum"
        :page-sizes="[1, 5, 10, 15, 20]"
        :page-size="queryModel.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getGoods, GoodsDelete } from "@/api/user";
import { Message, MessageBox } from "element-ui";
export default {
  data() {
    return {
      tableData: [],
      queryModel: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      total: 0,
    };
  },
  created() {
    this.getGoodList();
  },
  methods: {
    //添加商品
    addGoods() {
      this.$router.push("/goods/add");
    },
    //获取商品列表
    async getGoodList() {
      const { goods, total } = await getGoods(this.queryModel);
      this.tableData = goods;
      this.total = total;
      console.log(goods);
      // console.log(total);
      Message.success("获取数据成功");
    },
    // 分页
    handleSizeChange(val) {
      this.queryModel.pagesize = val;
      this.getGoodList();
    },
    handleCurrentChange(val) {
      this.queryModel.pagenum = val;
      this.getGoodList();
    },
    //删除
    delt(goods_id) {
      MessageBox.confirm("此操作将永久删除", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await GoodsDelete(goods_id);
          this.getGoodList();
          Message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          Message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style scoped >
.button {
  margin-left: 20px;
}
.el-table {
  margin-top: 20px;
}
</style>
